// 设备环境检测工具
class DeviceDetector {
  constructor() {
    this.userAgent = navigator.userAgent.toLowerCase()
  }

  // 检测设备类型
  detectDevice() {
    const ua = this.userAgent
    
    if (ua.includes('iphone') || ua.includes('ipad')) {
      return {
        type: 'ios',
        name: 'iOS设备',
        version: this.getIOSVersion(ua),
        isMobile: true
      }
    } else if (ua.includes('android')) {
      return {
        type: 'android', 
        name: '安卓设备',
        version: this.getAndroidVersion(ua),
        isMobile: true
      }
    } else {
      return {
        type: 'desktop',
        name: '桌面设备',
        isMobile: false
      }
    }
  }

  // 检测运行环境
  detectEnvironment() {
    const ua = this.userAgent
    
    if (ua.includes('micromessenger')) {
      return {
        env: 'wechat',
        name: '微信浏览器',
        isWeChat: true
      }
    } else if (ua.includes('mqqbrowser')) {
      return {
        env: 'qq',
        name: 'QQ浏览器', 
        isQQ: true
      }
    } else {
      return {
        env: 'browser',
        name: '标准浏览器',
        isWeChat: false,
        isQQ: false
      }
    }
  }

  // 获取iOS版本
  getIOSVersion(ua) {
    const match = ua.match(/os (\d+)_(\d+)_?(\d+)?/)
    if (match) {
      return `${match[1]}.${match[2]}.${match[3] || 0}`
    }
    return 'unknown'
  }

  // 获取安卓版本  
  getAndroidVersion(ua) {
    const match = ua.match(/android (\d+)/)
    if (match) {
      return match[1]
    }
    return 'unknown'
  }

  // 获取完整环境信息
  getFullEnvironment() {
    const device = this.detectDevice()
    const env = this.detectEnvironment()
    
    return {
      ...device,
      ...env,
      userAgent: navigator.userAgent,
      timestamp: new Date().toISOString(),
      language: navigator.language,
      platform: navigator.platform
    }
  }

  // 检查是否支持AirPlay
  checkAirPlaySupport() {
    const video = document.createElement('video')
    return {
      supported: !!(video.webkitShowPlaybackTargetPicker),
      hasAirPlayAPI: !!(video.webkitShowPlaybackTargetPicker),
      isIOS: this.detectDevice().type === 'ios',
      message: this.detectDevice().type === 'ios' ? 
        (video.webkitShowPlaybackTargetPicker ? '支持AirPlay' : 'iOS设备但不支持AirPlay API') :
        '非iOS设备，不支持AirPlay'
    }
  }

  // 输出环境信息
  logEnvironment() {
    const env = this.getFullEnvironment()
    const airplay = this.checkAirPlaySupport()
    
    console.log('📊 设备环境信息:', env)
    console.log('🎯 AirPlay支持:', airplay)
    
    return { env, airplay }
  }
}

// 创建全局实例
const deviceDetector = new DeviceDetector()

export default deviceDetector